---
title: closeDocument
description: Learn how to use the closeDocument method in @embedpdf/engines to free up memory.
searchable: true
---

# closeDocument

Closes a previously opened PDF document, freeing up all associated memory and resources within the WASM engine.

## Signature

```typescript
closeDocument(doc: PdfDocumentObject): PdfTask<boolean>;
````

## Description

It's **crucial** to call this method when you are finished with a document. Failing to do so will result in a memory leak, as the entire PDF and its associated data structures will remain allocated in the WebAssembly heap.

Once a document is closed, its `PdfDocumentObject` handle becomes invalid and should not be used for any further operations.

## Parameters

| Name | Type | Description |
| :--- | :--- | :--- |
| **`doc`** | `PdfDocumentObject` | The handle of the document to close, which you received from `openDocumentUrl` or `openDocumentFromBuffer`. |

## Returns

`PdfTask<boolean>`

A **`Task`** that resolves with `true` if the document was closed successfully. It will be rejected if the document handle is invalid or was already closed.

See [Concepts: Tasks](https://www.google.com/search?q=../concepts/tasks.md) for more on how to handle asynchronous operations.

## Example

```typescript
// Assuming 'engine' and an open 'document' object are available

console.log('Finished working with the document.');

try {
  await engine.closeDocument(document).toPromise();
  console.log('Document closed and resources freed successfully.');
} catch (error) {
  console.error('Failed to close the document:', error);
}

// Any subsequent calls using the 'document' handle will now fail.
// For example: await engine.renderPage(document, ...).toPromise(); -> REJECTION
```

## See Also

  - [Open Document from Buffer](open-document-buffer.md)
  - [Open Document from URL](open-document-url.md)